#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "Switch.h"
#include "Buzzer.h"
#include "Light.h"
#include "OLED.h"
// #include "Light_Interrput.h"
#include "NOLED.h"
#include "string.h"
#include "Timer.h"
#include "Pwm.h"
#include "Car.h"
#include "Usart.h"
#include "stdio.h"



int main(void)
{
	NOLED_Init();
	NOLED_ShowLineString(0,0,"hello");
	Usart_Init_TR();
	// Usart_Init();
	// Usart_SendByte(0x52);
	// Usart_SendByte('A');
	// Usart_SendByte('c');
	uint16_t Array[] = {'H', 'e', 'l', 'l','o',',','u','s','a','r','t'};
	// Usart_SendArray(&Array, 11);
	// Usart_SendString(" i'm a string!");
	// printf("开始接收数据 %s\r\n", "...");

	// Usart_SendHexPacket(&Array, 11);
	Usart_SetReceiveMode(4);
	// uint16_t Commands[] = {0x23,0x31,0x50,0x31, 0x35, 0x30, 0x30, 0x54, 0x31, 0x30, 0x30, 0x0D, 0x0A};
	uint8_t CommandIdx = 0;
	uint16_t Commands2[][6] = {
		{0,1250,1500,1500,1500,1500},
		{0,1250,2050,500,2500,1500},
		{0,1250,2050,500,2500,2500},
		{1250,1250,1500,500,2500,2500},
		{1250,1250,2050,500,2500,1500},
		{1250,1250,1500,500,1500,1500},
		{0,1250,1500,500,1500,1500},
	};
	uint16_t Commands[][6] = {
		{0,1250,1500,1500,1500,0},
		{0,1250,1500,1500,1500,0},
		{0,1250,1500,1500,1500,1500},
		{0,1250,1500,1500,1500,0},
		{0,1250,1500,1500,1500,1500},
		{0,1250,1500,1500,1500,0},
		{0,1250,1500,1500,1500,1500},
	};
    Delay_s(2);
    // Usart_SendRobotCommand(Commands[0][0], Commands[0][1], Commands[0][2], Commands[0][3], Commands[0][4], Commands[0][5]);

    Delay_s(2);
    while(1)
	{
        Usart_SendRobotCommand(Commands[0][0], Commands[0][1], Commands[0][2], Commands[0][3], Commands[0][4], Commands[0][5]);
		Delay_s(1);
        Usart_SendRobotCommand(Commands[1][0], Commands[1][1], Commands[1][2], Commands[1][3], Commands[1][4], Commands[1][5]);
		Delay_s(1);
        // CommandIdx++;
        // Usart_SendRobotCommand(Commands[CommandIdx][0], Commands[CommandIdx][1], Commands[CommandIdx][2], Commands[CommandIdx][3], Commands[CommandIdx][4], Commands[CommandIdx][5]);
		// Delay_s(1);
        // if (CommandIdx >= 7) {
		// 	for (; CommandIdx > 1; CommandIdx--)
		// 	{
        // 	Usart_SendRobotCommand(Commands[CommandIdx][0], Commands[CommandIdx][1], Commands[CommandIdx][2], Commands[CommandIdx][3], Commands[CommandIdx][4], Commands[CommandIdx][5]);
		// 	Delay_s(1);
		// 	}
        // }


    // Usart_SendArray(&Commands, 13);
		// if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
		// {

		// 	// uint16_t receive = USART_ReceiveData(USART1);
		// 	printf("循环接收到数据:\n\r");
		// 	// Usart_SendByte(receive);
		// 	// NOLED_ShowChar(1,0, receive);
		// }
		if (GetReceiveFlag() == 1)
		{
            if (Usart_GetReceiveMode() == 0) {
                printf("中断接收到数据");
                Usart_SendByte(GetReceive());
            }
            if (Usart_GetReceiveMode() == 1) {
                printf("接收到hex数据包");
                uint8_t *Receive = Usart_GetReceivePacket();
                Usart_SendHexPacket(Receive, 4);
                NOLED_ShowLineNumber(0, 0, Receive[0]);
                NOLED_ShowLineNumber(1, 0, Receive[1]);
                NOLED_ShowLineNumber(2, 0, Receive[2]);
                NOLED_ShowLineNumber(3, 0, Receive[3]);
            }
            if (Usart_GetReceiveMode() == 2) {
                printf("receive utf8 data:\r\n");
                uint8_t *Receive = Usart_GetReceivePacket();

                Usart_SendString(Receive);
				NOLED_ShowLineString(0,0, Receive);
            }
			if (Usart_GetReceiveMode() == 4)
			{
				Delay_s(1);
				Usart_SendRobotCommand(Commands[CommandIdx][0],
					Commands[CommandIdx][1],
					Commands[CommandIdx][2],
					Commands[CommandIdx][3],
					Commands[CommandIdx][4],
					Commands[CommandIdx][5]);
					CommandIdx++;
					if (CommandIdx == 5)
					{
						CommandIdx = 0;
					}

			}
        }
			// printf("接收数据完毕1:");
			// printf("接收数据完毕2:");
		

	}

}
